package com.lemon.springboot.service.impl;

import com.lemon.springboot.dao.ICpuDao;
import com.lemon.springboot.dao.IDiskDao;
import com.lemon.springboot.dao.IMachineDao;
import com.lemon.springboot.dao.IMemoryDao;
import com.lemon.springboot.entity.*;
import com.lemon.springboot.service.IIndexPageService;
import com.lemon.springboot.utils.DateUtil;
import com.lemon.springboot.utils.NumberUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author liuli
 * @Date 2020/12/17 8:31
 */
@Service
public class IndexPageServiceImpl implements IIndexPageService {
    private static Logger logger = LoggerFactory.getLogger(IndexPageServiceImpl.class);

    @Autowired
    ICpuDao cpuDao;

    @Autowired
    IMachineDao machineDao;

    @Autowired
    IDiskDao diskDao;

    @Autowired
    IMemoryDao memoryDao;

    @Override
    public List<OverviewVo> getOverviewData(String account) {
        List<OverviewVo> overviewVoList = new ArrayList<>();
        List<MachineVo> machineVoList = machineDao.findMachineByAcc(account);
        if (machineVoList != null && machineVoList.size() > 0) {
            // 获取token的集合
            List<String> tokenList = new ArrayList<>();
            for (MachineVo machineVo : machineVoList) {
                tokenList.add(machineVo.getUploadToken());
            }
            //获取最近的cpu使用情况
            List<CpuVo> cpuVoList = cpuDao.findLastCpuByTokenList(tokenList);
            //整理取到的数据 转成map token -> cpuVo
            Map<String, CpuVo> cpuVoMap = new HashMap<>();
            Map<String, DiskVo> diskVoMap = new HashMap<>();
            Map<String, MemoryVo> memoryVoMap = new HashMap<>();
            if (cpuVoList != null && cpuVoList.size() > 0) {
                for (CpuVo cpuVo : cpuVoList) {
                    cpuVoMap.put(cpuVo.getUploadToken(), cpuVo);
                }
            }
            // 查询最近硬盘使用情况
            List<DiskVo> diskVoList = diskDao.findLastDiskByTokenList(tokenList);
            if (diskVoList != null && diskVoList.size() > 0) {
                for (DiskVo diskVo : diskVoList) { // 这里硬盘数据要求和 因为硬盘数据可能有多条
                    sumDisk(diskVoMap, diskVo);
                }
            }
            // 查询最近内存使用情况
            List<MemoryVo> memoryVoList = memoryDao.findLastMemoryByTokenList(tokenList);
            if (memoryVoList != null && memoryVoList.size() > 0) {
                for (MemoryVo memoryVo : memoryVoList) {
                    memoryVoMap.put(memoryVo.getUploadToken(), memoryVo);
                }
            }

            for (MachineVo machineVo : machineVoList) {
                OverviewVo overviewVo = new OverviewVo();
                CpuVo cpuVo = cpuVoMap.get(machineVo.getUploadToken());
                DiskVo diskVo = diskVoMap.get(machineVo.getUploadToken());
                MemoryVo memoryVo = memoryVoMap.get(machineVo.getUploadToken());
                overviewVoList.add(overviewVo);
                overviewVo.setOsName(machineVo.getOsName());
                overviewVo.setPcName(machineVo.getPcName());
                overviewVo.setDataToken(machineVo.getUploadToken());
                if (cpuVo != null) {
                    overviewVo.setCpuModel(cpuVo.getCpuModel());
                    overviewVo.setCpuUsage(NumberUtil.floatFormat(((double) cpuVo.getCpuUsage()) / 100, "0.00"));
                    overviewVo.setDateStr(DateUtil.dateFormat(cpuVo.getDate(), "yyyy-MM-dd HH:mm:ss"));
                }
                if (diskVo != null) {
                    // 磁盘空间转成GB
                    String diskTotal = NumberUtil.floatFormat(((double) diskVo.getDiskTotal()) / 1024 / 1024, "0.0");
                    String diskUse = NumberUtil.floatFormat(((double) diskVo.getDiskUse()) / 1024 / 1024, "0.0");
                    overviewVo.setDiskTotal(diskTotal);
                    overviewVo.setDiskUse(diskUse);
                }
                if (memoryVo != null) {
                    // 内存转GB
                    String memoryTotal = NumberUtil.floatFormat(((double) memoryVo.getMemoryTotal()) / 1024 / 1024, "0.0");
                    String memoryUse = NumberUtil.floatFormat(((double) memoryVo.getMemoryUse()) / 1024 / 1024, "0.0");
                    overviewVo.setMemoryUse(memoryUse);
                    overviewVo.setMemoryTotal(memoryTotal);
                }
            }
        }
        return overviewVoList;
    }

    private void sumDisk(Map<String, DiskVo> diskVoMap, DiskVo diskVo) {
        if (diskVoMap.containsKey(diskVo.getUploadToken())) {
            DiskVo disk = diskVoMap.get(diskVo.getUploadToken());
            disk.setDiskTotal(disk.getDiskTotal() + diskVo.getDiskTotal());
            disk.setDiskUse(disk.getDiskUse() + diskVo.getDiskUse());
        } else {
            diskVoMap.put(diskVo.getUploadToken(), diskVo);
        }
    }
}
